﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;



namespace DoAn
{

    public class MySprite
    {
        private int _type;
        public int TypeMySprite
        {
            get { return _type; }
            set { _type = value; }
        }

        private int _flag;
        public int Flag
        {
            get { return _flag; }
            set { _flag = value; }
        }

        private Texture2D[] _texture;
        public Texture2D[] Texture
        {
            get { return _texture; }
            set
            {
                _texture = value;
                _nTexture = _texture.Length;
                _iTexture = 0;
            }
        }

        private int _iTexture;
        public int iTexture
        {
            get { return _iTexture; }
            set { _iTexture = value; }
        }


        private int _nTexture;
        public int nTexture
        {
            get { return _nTexture; }
            set { _nTexture = value; }
        }

        private int _level=1;
        public int Level
        {
            get { return _level; }
            set 
            {
                if (value > 1 && value <= 10)
                {
                    Uplevel(value);
                    _level = value;
                    //if(Damage*0.1 > 0)
                    //    Damage += (int)(Damage * 0.1);
                    //else
                    //    Damage++;
                    //Armor += 1;
                    //Blood += (int)(MaxBlood * 0.2);
                    //MaxBlood += (int)(MaxBlood * 0.1);
                    //Blood = Math.Min(MaxBlood, Blood);
                }
            }
        }

        private int _score;
        public int Score
        {
            get { return _score; }
            set 
            { 
                _score = value;
                int d = 800 + Level * 200;
                if (_score >= d && Level <= 10)
                {
                    _score -= d;
                    Level++;
                }
            }
        }

        private int _blood;
        public int Blood
        {
            get { return _blood; }
            set
            {
                _blood = Math.Min(value, MaxBlood);
                if (_blood <= 0)
                {
                    Life = false;
                    Game1.uncheckCell(Game1.center(PosCenter));
                    if (TypeMySprite == 6)
                    {
                        Vector2 v = Position;
                        MainHome.UnCheckCell(ref v);
                    }
                }
            }
        }

        private int _maxBlood;
        public int MaxBlood
        {
            get { return _maxBlood; }
            set { _maxBlood = value; }
        }

        private Texture2D _texture2DBlood;
        public Texture2D Texture2DBlood
        {
            get { return _texture2DBlood; }
            set { _texture2DBlood = value; }
        }

        private bool life;
        public bool Life
        {
            get { return life; }
            set { life = value; }
        }

        private int _armor;
        public int Armor
        {
            get { return _armor; }
            set { _armor = value; }
        }

        private int _damage;
        public int Damage
        {
            get { return _damage; }
            set { _damage = value; }
        }

        private Vector2 _position;
        public Vector2 Position
        {
            get { return _position; }
            set
            {
                _position = value;
                PosCenter = Position + Size / 2;
                ActualPos = Position + (Size - ActualSize) / 2;
                CreateDistanceGo();
            }
        }
        public Vector2 Position2
        {
            get { return _position; }
            set
            {
                _position = value;
                PosCenter = Position + Size / 2;
                ActualPos = Position + (Size - ActualSize) / 2;
                //DistanceGo2 = new Vector2(0,0);
            }
        }

        private Vector2 _posCenter;

        public Vector2 PosCenter
        {
            get { return _posCenter; }
            set { _posCenter = value; }
        }
        private Vector2 _DistanceGo;

        public Vector2 DistanceGo
        {
            get { return _DistanceGo; }
            set
            {
                _DistanceGo = value;
                if (!Attack)
                    ChooseDynamic(DistanceGo, 2);
            }
        }
        public Vector2 DistanceGo2
        {
            get { return _DistanceGo; }
            set
            {
                _DistanceGo = value;
            }
        }

        private Vector2 _Target;
        public Vector2 Target
        {
            get { return _Target; }
            set
            {
                if (value.X >= Game1.mapwithcells.Topleft.X && value.X <= Game1.mapwithcells.Topleft.X + MapWithCells.WIDTHMAP
                    && value.Y >= Game1.mapwithcells.Topleft.Y && value.Y <= Game1.mapwithcells.Topleft.Y + MapWithCells.HEIGHTMAP)
                {
                    Vector2 v = Game1.center(value);
                    _Target = new Vector2(v.X - Size.X / 2, v.Y - Size.Y / 2);
                    CreateDistanceGo();
                    //Game1.uncheckCell(Game1.center(PosCenter));
                }
            }
        }
        public Vector2 Target2
        {
            get { return _Target; }
            set
            {
                if (value.X >= Game1.mapwithcells.Topleft.X && value.X <= Game1.mapwithcells.Topleft.X + MapWithCells.WIDTHMAP
                    && value.Y >= Game1.mapwithcells.Topleft.Y && value.Y <= Game1.mapwithcells.Topleft.Y + MapWithCells.HEIGHTMAP)
                {
                    Vector2 v = Game1.center(value);
                    //Vector2 v = value;
                    _Target = new Vector2(v.X - Size.X / 2, v.Y - Size.Y / 2);
                }
            }
        }

        public void CreateDistanceGo()
        {
            if (TypeMySprite < 6)
            {
                Vector2 dist = Target - Position;
                float k = dist.Length();
                if (k != 0)
                {
                    //DistanceGo = new Vector2((distX / k) * 3, (distY / k) * 3);
                    DistanceGo = 2 * (dist / k);
                }
                else
                {
                    DistanceGo = new Vector2(0);
                    if (!Attack)
                        DynamicState(0, Direction);
                }
            }
        }
        private Vector2 _actualSize;
        public Vector2 ActualSize
        {
            get { return _actualSize; }
            set { _actualSize = value; }
        }

        private Vector2 _actualPos;
        public Vector2 ActualPos
        {
            get { return _actualPos; }
            set { _actualPos = value; }
        }

        protected Vector2 _size;
        public Vector2 Size
        {
            get { return _size; }
            set
            {
                _size = value;
                if(TypeMySprite < 6)
                    ActualSize = Size * 0.6f;
                else
                    ActualSize = Size * 0.8f;
            }
        }

        //Đang cử động gì
        private int _State = 0;
        public int State
        {
            get { return _State; }
            set { _State = value; }
        }
        private int _Direction = 0;
        public int Direction
        {
            get { return _Direction; }
            set { _Direction = value; }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Rectangle rect)
        {
            spriteBatch.Draw(Texture[iTexture], rect, Color.White);
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (Life)
            {
                if (Flag == 1)
                {
                    spriteBatch.Draw(Texture[iTexture], Position, Color.White);

                }
                else
                {
                    spriteBatch.Draw(Texture[iTexture], Position, Color.Red);
                }
            }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Rectangle rect, Rectangle rectblood, Rectangle rectMaxblood)
        {
            if (Life)
            {
                if (Flag == 1)
                {
                    spriteBatch.Draw(Texture[iTexture], rect, Color.White);
                    spriteBatch.Draw(Texture2DBlood, rectMaxblood, Color.White);
                    spriteBatch.Draw(Texture2DBlood, rectblood, Color.Green);
                }
                else
                {
                    spriteBatch.Draw(Texture[iTexture], rect, Game1.skin);
                    spriteBatch.Draw(Texture2DBlood, rectMaxblood, Color.White);
                    spriteBatch.Draw(Texture2DBlood, rectblood, Color.Green);
                }
            }
        }
        //Sprite Icon
        public void Draw(SpriteBatch spriteBatch, int flag)
        {
            Rectangle rect = new Rectangle((int)Position.X, (int)Position.Y, (int)Size.X, (int)Size.Y);
            if(flag == 0)
                spriteBatch.Draw(Texture[iTexture], rect, Color.Red);
            else
                spriteBatch.Draw(Texture[iTexture], rect, Color.White);
        }
        private int _delay = 64;
        public int Delay
        {
            get { return _delay; }
            set { _delay = value; }
        }
        int count = 0;
        public void Update(GameTime gameTime)
        {
            if (Life)
            {
                count++;
                if (TypeMySprite < 6)//Nếu là lính
                {
                    if (NumberofStates == 1)
                        State = 0;
                    Update(gameTime, (State * 64) + (Direction * 8), 8);
                }
                else//Nếu là nhà 
                {
                    if (TypeMySprite == 6)//Barrack
                    {
                        //Position = MainHome.Position;
                        Vector2 v = Position;
                        MainHome.CheckCell(ref v);
                    }
                    else
                    {
                        if (TypeMySprite == 7)//GoldBuilding
                        {
                            if (count % 150 == 0)
                                Game1.gold += 10;
                            Vector2 v = Position;
                            GoldBuilding.CheckCell(ref v);
                        }
                    }
                }
            }
        }
        public void Update(GameTime gameTime, int idx, int n)
        {
            if (Life)
            {
                if (count % Delay == 0)
                    iTexture = ((iTexture + 1) % n) + idx;
            }
        }
        //Sprite Icon
        public void Update()
        {
            count++;
            if (count % Delay == 0)
                iTexture = (iTexture + 1) % nTexture;
        }
        public void DynamicState(int idx, int n)
        {
            State = idx;
            Direction = n;
        }

        public MySprite(Texture2D[] texture, Vector2 pos, Vector2 size, int delay, int armor, int damage, int flag, Texture2D bloodremain, int blood, int numState, int type)
        {
            Texture = texture;
            Size = size;
            Position = pos;
            Delay = delay;
            _Target = pos;
            Life = true;
            Armor = armor;
            Damage = damage;
            Flag = flag;
            Texture2DBlood = bloodremain;
            MaxBlood = blood;
            Blood = blood;
            Level = 1;
            NumberofStates = numState;
            TypeMySprite = type;
        }
        public MySprite(MySprite temp)
        {
            Texture = temp.Texture;
            Size = temp.Size;
            Position = temp.Position;
            Delay = temp.Delay;
            _Target = temp.Target;
            Life = true;
            Armor = temp.Armor;
            Damage = temp.Damage;
            Flag = temp.Flag;
            Texture2DBlood = temp.Texture2DBlood;
            MaxBlood = temp.MaxBlood;
            Blood = temp.Blood;
            Level = temp.Level;
            NumberofStates = temp.NumberofStates;
            TypeMySprite = temp.TypeMySprite;
        }
        //Sprite Icon
        public MySprite(ContentManager content, String str, int npic, Vector2 pos, Vector2 size, int delay)
        {
            Size = size;
            Position = pos;
            Texture = new Texture2D[npic];
            nTexture = npic;
            for (int i = 0; i < npic; i++)
            {
                Texture[i] = content.Load<Texture2D>(str + (i + 1).ToString());
            }
            Delay = delay;
        }
        //Địa hình
        public MySprite(ContentManager content, String str, Vector2 pos, Vector2 size, int delay)
        {
            Size = size;
            Position = pos;
            Texture = new Texture2D[1];
            nTexture = 1;
            for (int i = 0; i < 1; i++)
            {
                Texture[i] = content.Load<Texture2D>(str.ToString());
            }
            Delay = delay;
        }
        public bool IsClick(Vector2 pos)
        {
            if (Life)
            {
                if ((Position.X < pos.X) && (Position.X + Size.X > pos.X) &&
                    (Position.Y < pos.Y) && (Position.Y + Size.X > pos.Y))
                    return true;
            }
            return false;
        }

        public bool IsFocus(Rectangle rec)
        {
            if (Life)
            {
                if ((ActualPos.X < rec.Right) && (ActualPos.X + ActualSize.X > rec.Left) &&
                    (ActualPos.Y < rec.Bottom) && (ActualPos.Y + ActualSize.Y > rec.Top))
                    return true;
            }
            return false;
        }

        private int numberofStates;
        public int NumberofStates
        {
            get { return numberofStates; }
            set { numberofStates = value; }
        }

        public void ChooseDynamic(Vector2 vec, int st)
        {
            if (Life)
            {
                vec = vec / vec.Length();
                float x = vec.X;// / 3;
                float y = vec.Y;// / 3;
                if (x > 0.866f)
                {
                    DynamicState(st, 0);
                }
                if (x < 0.866f && x >= 0.5f && y > 0)
                {
                    DynamicState(st, 1);
                }
                if (x < 0.5f && x >= -0.5f && y > 0)
                {
                    DynamicState(st, 2);
                }
                if (x <= -0.5f && x > -0.866f && y > 0)
                {
                    DynamicState(st, 3);
                }
                if (x <= -0.866f)
                {
                    DynamicState(st, 4);
                }
                if (x <= -0.5f && x > -0.866f && y < 0)
                {
                    DynamicState(st, 5);
                }
                if (x > -0.5 && x <= 0.5f && y < 0)
                {
                    DynamicState(st, 6);
                }
                if (x <= 0.866f && x > 0.5f && y < 0)
                {
                    DynamicState(st, 7);
                }
            }
        }

        public void avoidSprite(MySprite avoid)
        {
            if (avoid.Life)
            {
                double AngleTarget;
                double AngleAvoid;
                Vector2 realTarget;
                float AbsX;
                float AbsY;
                float X;
                Vector2 avoidVector = avoid.PosCenter;
                Vector2 k = PosCenter - avoidVector;
                AbsX = Math.Abs(k.X);
                AbsY = Math.Abs(k.Y);
                X = k.X;
                float Y = k.Y;
                Vector2 t;
                realTarget = (Target + Size / 2) - PosCenter;
                Vector2 vectorAvoid = avoidVector - PosCenter;
                t = realTarget;
                t.Y = -t.Y;
                realTarget = t;
                t = vectorAvoid;
                t.Y = -t.Y;
                vectorAvoid = t;
                AngleTarget = (Math.Atan((double)(realTarget.Y / (realTarget.X)))) * 180 / 3.14;
                AngleAvoid = Math.Atan((double)(vectorAvoid.Y / (vectorAvoid.X))) * 180 / 3.14;
                /////////////////////////Xử lý góc
                if (realTarget.X > 0 && realTarget.Y > 0)
                    AngleTarget = AngleTarget + 0;
                else
                    if (realTarget.X < 0 && realTarget.Y > 0)
                        AngleTarget = 180 + AngleTarget;
                    else
                    {
                        if (realTarget.X < 0 && realTarget.Y < 0)
                            AngleTarget = 180 + AngleTarget;
                        else
                            if (realTarget.X > 0 && realTarget.Y < 0)
                                AngleTarget = 360 + AngleTarget;
                    }
                if (vectorAvoid.X > 0 && vectorAvoid.Y > 0)
                    AngleAvoid = AngleAvoid + 0;
                else
                    if (vectorAvoid.X < 0 && vectorAvoid.Y > 0)
                        AngleAvoid = 180 + AngleAvoid;
                    else
                    {
                        if (vectorAvoid.X < 0 && vectorAvoid.Y < 0)
                            AngleAvoid = 180 + AngleAvoid;
                        else
                            if (vectorAvoid.X > 0 && vectorAvoid.Y < 0)
                                AngleAvoid = 360 + AngleAvoid;
                    }
                /////////////////////////

                //Xử lý collision
                if (AbsX >= AbsY && X < 0)//từ trái sang phải
                {
                    if (AngleTarget > 270 && AngleAvoid > 270 || AngleTarget < 90 && AngleAvoid < 90)
                    {
                        if (AngleTarget >= AngleAvoid)
                        {
                            DistanceGo = new Vector2(-1, -3);
                            //avoid.Target += new Vector2(0, 3) + Size/2;
                            //avoid.DistanceGo += new Vector2(0, 3);
                        }
                        else
                        {
                            DistanceGo = new Vector2(-1, 3);
                            //avoid.Target += new Vector2(0, -3) + Size / 2;
                            //avoid.DistanceGo += new Vector2(0, -3);
                        }
                    }
                    else
                    {
                        if (AngleTarget > 270 && AngleAvoid < 90 || AngleTarget < 90 && AngleAvoid > 270)
                        {
                            if (AngleTarget >= AngleAvoid)//AngleTarget > 270 && AngleAvoid > 270)
                            {
                                DistanceGo = new Vector2(-1, 3);
                                //avoid.Target += new Vector2(0, -3) + Size / 2;
                                //avoid.DistanceGo += new Vector2(0, -3);
                            }
                            else
                            {
                                DistanceGo = new Vector2(-1, -3);
                                //avoid.Target += new Vector2(0, 3) + Size / 2;
                                //avoid.DistanceGo += new Vector2(0, 3);
                            }
                        }
                    }
                }
                else
                {
                    if (AbsX >= AbsY && X > 0)//từ phải sang trái
                    {
                        if (AngleTarget >= AngleAvoid)
                        {
                            DistanceGo = new Vector2(1, 3);
                            //avoid.Target += new Vector2(0, -3) + Size / 2;
                            //avoid.DistanceGo += new Vector2(0, -3);
                        }
                        else
                        {
                            DistanceGo = new Vector2(1, -3);
                            //avoid.Target += new Vector2(0, 3) + Size / 2;
                            //avoid.DistanceGo += new Vector2(0, 3);
                        }
                    }
                    else
                    {
                        if (AbsX <= AbsY && Y < 0)//từ trên xuống dưới
                        {
                            if (AngleTarget >= AngleAvoid)
                            {
                                DistanceGo = new Vector2(3, -1);
                                //avoid.Target += new Vector2(-3, 0) + Size / 2;
                                //avoid.DistanceGo += new Vector2(-3, 0);
                            }
                            else
                            {
                                DistanceGo = new Vector2(-3, -1);
                                //avoid.Target += new Vector2(3, 0) + Size / 2;
                                //avoid.DistanceGo += new Vector2(3, 0);
                            }
                        }
                        else
                        {
                            if (AbsX <= AbsY && Y > 0)//từ dưới lên trên
                            {
                                if (AngleTarget >= AngleAvoid)
                                {
                                    DistanceGo = new Vector2(-3, 1);
                                    //avoid.Target += new Vector2(3, 0) + Size / 2;
                                    //avoid.DistanceGo += new Vector2(3, 0);
                                }
                                else
                                {
                                    DistanceGo = new Vector2(3, 1);
                                    //avoid.Target += new Vector2(-3, 0) + Size / 2;
                                    //avoid.DistanceGo += new Vector2(-3, 0);
                                }
                            }
                        }
                    }
                }
            }
        }


        public void avoidSpriteStatic(MySprite avoid)
        {
            if (avoid.Life)
            {
                Vector2 k = PosCenter - avoid.PosCenter;
                int d = 4;//khoảng cách tự tránh nhau
                if (k.Length() < 3)
                {
                    Random r = new Random();
                    DistanceGo = new Vector2(d * (r.Next(-2, 2)), d * (r.Next(-2, 2)));
                }
                else
                {

                    float AbsX = Math.Abs(k.X);
                    float AbsY = Math.Abs(k.Y);
                    float X = k.X;
                    float Y = k.Y;
                    //Xử lý collision
                    if (AbsX >= AbsY && X < 0)//từ trái sang phải
                    {
                        if (Y <= 0)
                            DistanceGo = new Vector2(0, -d);
                        else
                            DistanceGo = new Vector2(0, d);
                    }
                    else
                    {
                        if (AbsX >= AbsY && X > 0)//từ phải sang trái
                        {
                            if (Y <= 0)
                                DistanceGo = new Vector2(0, -d);
                            else
                                DistanceGo = new Vector2(0, d);
                        }
                        else
                        {
                            if (AbsX <= AbsY && Y < 0)//từ trên xuống dưới
                            {
                                if (X <= 0)
                                    DistanceGo = new Vector2(-d, 0);
                                else
                                    DistanceGo = new Vector2(d, 0);
                            }
                            else
                            {
                                if (AbsX <= AbsY && Y > 0)//từ dưới lên trên
                                {
                                    if (X <= 0)
                                        DistanceGo = new Vector2(-d, 0);
                                    else
                                        DistanceGo = new Vector2(d, 0);
                                }
                            }
                        }
                    }
                }
            }
        }
        private bool _flagAttack = true;
        public bool FlagAttack
        {
            get { return _flagAttack; }
            set { _flagAttack = value; }
        }
        private bool _attack = false;
        public bool Attack
        {
            get { return _attack; }
            set { _attack = value; }
        }
        private MySprite _beAttacker;
        public MySprite BeAttacker
        {
            get { return _beAttacker; }
            set { _beAttacker = value; }
        }
        //bool redAttack = false;
        //public bool RedAttack
        //{
        //    get { return redAttack; }
        //    set { redAttack = value; }
        //}
        public bool PreAttack(MySprite ob)
        {
            int d = Game1.Dis_Soldier_Deadman(PosCenter, ob.PosCenter);
            int d2 = Game1.CellHeight * 2;
            if (ob.TypeMySprite > 5)
                d2 += Game1.CellHeight;
            if (d < d2)
                return true;
            return false;
        }
        public void BeginAttack(MySprite ob)
        {
            //if (!PreAttack(ob) && BeAttacker == ob)//Phát hiện kẻ địch từ xa
            if ((BeAttacker == ob && !PreAttack(ob)) || (Target != Position && this.FlagAttack))
            {
                Vector2 v = PosCenter - ob.PosCenter;
                v = 1.2f*Game1.CellWidth * v / v.Length();

                if (ob.TypeMySprite < 6)
                    v = ob.PosCenter + v;
                else
                {
                    if (ob.TypeMySprite == 6)
                    {
                       v = ob.PosCenter + 2 * v;
                    }
                    else
                    {
                        if (ob.TypeMySprite == 7)
                        {
                            v = ob.PosCenter + (1.5f) * v;
                        }
                    }
                }
                if (Game1.TestVector(v))//Nếu chỗ này đã có người đứng thì chọn ô khác đứng
                {
                    v = SubBeginAttack(v);
                }
                Target = v;
                Game1.uncheckCell(Game1.center(PosCenter));
            }
            if (Target == Position)//đứng lại chuẩn bị đánh địch
            {
                if (!Attack)//Nếu chưa đánh ai thì chọn người đánh
                {
                    BeAttacker = ob;
                    Attack = true;
                    ca = 0;
                }
                if (BeAttacker == ob)//Nếu đây là đối thủ mình đang đánh
                    Attacking(BeAttacker);
                else
                {
                    if (BeAttacker != null && BeAttacker.Life)
                    {
                        if (Dis_Soldier_Deadman(PosCenter, BeAttacker.PosCenter) > Game1.CellWidth * 4)//Nếu người mình đang đánh wá xa thì chọn đối thủ khác
                        {
                            FinishAttack();
                            BeginAttack(ob);
                            //BeAttacker = ob;
                            //Attack = true;
                            //Attacking(BeAttacker);
                        }
                    }
                    else
                    {
                        FinishAttack();
                    }
                }
            }
            else
            {
                Attack = false;
                BeAttacker = null;
                DynamicState(0, Direction);
            }
        }
        int ca = 0;
        public void Attacking(MySprite ob)
        {
            ca++;
            if (ob.Life)
            {
                Target = PosCenter;
                Vector2 v = ob.PosCenter - PosCenter;
                ChooseDynamic(v, 1);
                if (ca % 40 == 0)
                {
                    int result = Damage - ob.Armor;
                    if (result < 1)
                        result = 1;
                    ob.Blood -= result;
                    if (ob.Blood <= 0)
                    {
                        Game1.gold += ob.Level * 200;
                        Score += ob.Level * 200;
                        FinishAttack();
                    }
                }
            }
        }
        public void FinishAttack()
        {
            Attack = false;
            //RedAttack = false;
            BeAttacker = null;
            DynamicState(0, Direction);
        }

        public int Dis_Soldier_Deadman(Vector2 v1, Vector2 v2)
        {
            int d = (int)(v1 - v2).Length();
            return d;
        }
        //Chọn vị trí đứng để đánh
        private Vector2 SubBeginAttack(Vector2 v)
        {
            int x = (int)(v - PosCenter).X;
            int y = (int)(v - PosCenter).Y;
            if (Math.Abs(x) >= Math.Abs(y))
            {
                if (x >= 0 && y < 0)
                {
                    return v + new Vector2(0, Game1.CellWidth);
                }
                if (x >= 0 && y >= 0)
                {
                    return v + new Vector2(0, -Game1.CellWidth);
                }

                if (x < 0 && y < 0)
                {
                    return v + new Vector2(0, Game1.CellWidth);
                }
                if (x < 0 && y >= 0)
                {
                    return v + new Vector2(0, -Game1.CellWidth);
                }
                return v;
            }
            else
            {
                if (x >= 0 && y < 0)
                {
                    return v + new Vector2(-Game1.CellWidth, 0);
                }
                if (x < 0 && y < 0)
                {
                    return v + new Vector2(Game1.CellWidth, 0);
                }

                if (x >= 0 && y >= 0)
                {
                    return v + new Vector2(-Game1.CellWidth, 0);
                }
                if (x < 0 && y >= 0)
                {
                    return v + new Vector2(Game1.CellWidth, 0);
                }
                return v;
            }
        }

        //Luân thêm
        public MySprite(Texture2D[] texture, Vector2 pos)
        {
            Texture = texture;
            Position = pos;
        }
        public void DrawMap(GameTime gameTime, SpriteBatch spriteBatch, Rectangle rect)
        {
            spriteBatch.Draw(Texture[iTexture], rect, Color.White);
        }


        public void Uplevel(int lv)
        {
            int c = lv - _level;
            if (c > 0)
            {
                for (int i = 0; i < c; i++)
                {
                    subUplevel();
                }
            }
        }
        public void subUplevel()
        {
            if (Damage * 0.1 > 0)
                Damage += (int)(Damage * 0.1);
            else
                Damage++;
            Armor += 1;
            Blood += (int)(MaxBlood * 0.2);
            MaxBlood += (int)(MaxBlood * 0.1);
            Blood = Math.Min(MaxBlood, Blood);
        }
    }
}
